package com.ruoyi.patrol.service.impl;

import java.util.List;

import com.ruoyi.common.constant.PatrolConstans;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtLineGuard;
import com.ruoyi.patrol.domain.vo.PtSiteRecordVo;
import com.ruoyi.patrol.service.IPtLineGuardService;
import com.ruoyi.patrol.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtLineRecordDetailMapper;
import com.ruoyi.patrol.domain.PtLineRecordDetail;
import com.ruoyi.patrol.service.IPtLineRecordDetailService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 巡逻点位记录Service业务层处理
 *
 * @author mlb
 * @date 2022-11-16
 */
@Service
public class PtLineRecordDetailServiceImpl implements IPtLineRecordDetailService {
    @Autowired
    private PtLineRecordDetailMapper ptLineRecordDetailMapper;
    @Autowired
    private IPtLineGuardService ptLineGuardService;

    /**
     * 查询巡逻点位记录
     *
     * @param id 巡逻点位记录ID
     * @return 巡逻点位记录
     */
    @Override
    public PtLineRecordDetail selectPtLineRecordDetailById(Long id) {
        return ptLineRecordDetailMapper.selectPtLineRecordDetailById(id);
    }

    /**
     * 查询巡逻点位记录列表
     *
     * @param ptLineRecordDetail 巡逻点位记录
     * @return 巡逻点位记录
     */
    @Override
    public List<PtLineRecordDetail> selectPtLineRecordDetailList(PtLineRecordDetail ptLineRecordDetail) {
        List<PtLineRecordDetail> list = ptLineRecordDetailMapper.selectPtLineRecordDetailList(ptLineRecordDetail);
        list.forEach(item -> {
            if (!StringUtils.isEmpty(item.getPhotoUrl())) {
                String s = item.getPhotoUrl().split(".com\\/")[1];
                String ossUrl = OssUtils.getOssUrl(s);
                item.setPhotoUrl(ossUrl);
            }
        });
        return list;
    }

    /**
     * 新增巡逻点位记录
     *
     * @param ptLineRecordDetail 巡逻点位记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtLineRecordDetail(PtLineRecordDetail ptLineRecordDetail) {
//        // 巡逻点位异常时，巡查排班表中异常个数加1
//        PtLineGuard lineGuard = ptLineGuardService.selectPtLineGuardById(ptLineRecordDetail.getScheduleId());
//
//        PtLineRecordDetail query = new PtLineRecordDetail();
//        query.setScheduleId(ptLineRecordDetail.getScheduleId());
//        List<PtLineRecordDetail> details = ptLineRecordDetailMapper.selectPtLineRecordDetailList(query);
//
//        if (ptLineRecordDetail.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL) {
//            lineGuard.setPatrolResult(lineGuard.getPatrolResult() + 1);
//        }
//        // 如果当前排班表的巡查状态为“未开始”，则改为“巡查中”；如果提交的是该线路最后一个巡逻点位，则改为“已完成”
//        if (details.size() == 0) {
//            lineGuard.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DOING);
//        } else if (details.size() >= lineGuard.getSiteCount() - 1) {
//            lineGuard.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DONE);
//        }
//        ptLineGuardService.updatePtLineGuard(lineGuard);
//
//        ptLineRecordDetail.setCreateBy(SecurityUtils.getNickName());
//        ptLineRecordDetail.setCreateTime(DateUtils.getNowDate());
        return ptLineRecordDetailMapper.insertPtLineRecordDetail(ptLineRecordDetail);
    }

    /**
     * 修改巡逻点位记录
     *
     * @param record 巡逻点位记录
     * @return 结果
     */
    @Override
    public int updatePtLineRecordDetail(PtSiteRecordVo record) {
        PtLineRecordDetail detail = ptLineRecordDetailMapper.selectPtLineRecordDetailById(record.getRecordId());
        if (detail == null) {
            return 0;
        }

        String operator = SecurityUtils.getNickName();
        // 更新巡逻点位记录
        detail.setState(record.getState());
        detail.setPhotoUrl(record.getPhotoUrl());
        detail.setLatitude(record.getLatitude());
        detail.setLongitude(record.getLongitude());
        detail.setUpdateTime(DateUtils.getNowDate());
        detail.setUpdateBy(operator);
        detail.setRemark(record.getRemark());
        detail.setHandleInfo(record.getHandleInfo());
        ptLineRecordDetailMapper.updatePtLineRecordDetail(detail);

        PtLineGuard lineGuard = ptLineGuardService.selectPtLineGuardById(detail.getScheduleId());
        // 当巡逻点位异常时，更新线路的异常数量
        if (record.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL) {
            lineGuard.setPatrolResult(lineGuard.getPatrolResult() + 1);
        }

        // 更新线路的状态
        PtLineRecordDetail query = new PtLineRecordDetail();
        query.setScheduleId(detail.getScheduleId());
        List<PtLineRecordDetail> details = ptLineRecordDetailMapper.selectPtLineRecordDetailList(query);
        long undoCount = details.stream().filter(e -> e.getState() == PatrolConstans.PATROL_SITE_STATUES_UNDO).count();
        long abnormalCount = details.stream().filter(e -> e.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL).count();

        //未巡逻点位个数
        if (undoCount == 0) {
            lineGuard.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DONE);
        } else {
            lineGuard.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DOING);
        }
        lineGuard.setPatrolResult((int) abnormalCount); //巡查中异常巡逻点位的个数

        ptLineGuardService.updatePtLineGuard(lineGuard);

        return 1;
    }

    @Override
    public int handleAbnormal(PtSiteRecordVo record) {
        PtLineRecordDetail detail = ptLineRecordDetailMapper.selectPtLineRecordDetailById(record.getRecordId());
        if (detail == null) {
            return 0;
        }

        // 更新巡逻点位记录
        detail.setState(record.getState());
        detail.setHandleInfo(record.getHandleInfo());
        ptLineRecordDetailMapper.updatePtLineRecordDetail(detail);

        // 更新异常个数
        PtLineRecordDetail query = new PtLineRecordDetail();
        query.setScheduleId(detail.getScheduleId());
        List<PtLineRecordDetail> details = ptLineRecordDetailMapper.selectPtLineRecordDetailList(query);
        long abnormalCount = details.stream().filter(e -> e.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL).count();

        PtLineGuard lineGuard = ptLineGuardService.selectPtLineGuardById(detail.getScheduleId());
        lineGuard.setPatrolResult((int) abnormalCount);
        ptLineGuardService.updatePtLineGuard(lineGuard);

        return 1;
    }

    /**
     * 批量删除巡逻点位记录
     *
     * @param ids 需要删除的巡逻点位记录ID
     * @return 结果
     */
    @Override
    public int deletePtLineRecordDetailByIds(Long[] ids) {
        return ptLineRecordDetailMapper.deletePtLineRecordDetailByIds(ids);
    }

    /**
     * 删除巡逻点位记录信息
     *
     * @param id 巡逻点位记录ID
     * @return 结果
     */
    @Override
    public int deletePtLineRecordDetailById(Long id) {
        return ptLineRecordDetailMapper.deletePtLineRecordDetailById(id);
    }

    @Override
    public long selectCount(PtLineRecordDetail detail) {
        return ptLineRecordDetailMapper.selectCount(detail);
    }

    @Override
    public long selectLineFinishDayCount(Long schoolId) {
        return ptLineRecordDetailMapper.selectLineFinishDayCount(schoolId);
    }
}
